import { createRouter, createWebHistory } from "vue-router";
import AdminLogin from "../views/AdminLogin.vue";
import AdminRoom from "../views/AdminRoom.vue";
import ChatRoom from "../views/ChatRoom.vue";

const routes = [
  { path: "/", redirect: "/admin/login" },
  { path: "/admin/login", component: AdminLogin },
  { path: "/admin/rooms", component: AdminRoom },
  { path: "/chat/:roomId", component: ChatRoom }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 管理端路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("admin_token");
  if (to.path.startsWith("/admin") && to.path !== "/admin/login" && !token) {
    next("/admin/login");
  } else {
    next();
  }
});

export default router;
